package com.css.fxfzfxqh.modules.lifelineEngineering.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.AddTaskParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.FuzzyQueryParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.PageParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.service.TaskManagementService;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 任务管理
 */
@RestController
@RequestMapping("taskManagement")
@Slf4j
public class TaskManagementController {

    @Autowired
    private TaskManagementService taskManagementService;

    /**
     * 新建或修改
     */
    @PostMapping("saveOrUpdate")
    public RestResponse saveOrUpdate(@RequestBody AddTaskParam param) {
        try {
            if (PlatformObjectUtils.isNotEmpty(param.getId())) {
                if (taskManagementService.getByName(param.getName()) > 1) {
                    return RestResponse.fail("失败,任务名字重复!");
                }
            } else {
                if (taskManagementService.getByName(param.getName()) != 0) {
                    return RestResponse.fail("失败,任务名字重复!");
                }
            }
            taskManagementService.saveOrUpdate(param);
            return RestResponse.succeed("成功!");
        } catch (RuntimeException e) {
            log.error("post save error:{}", e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 分页
     */
    @GetMapping("page")
    public RestResponse page(HttpServletRequest request, PageParam param) {
        try {
            int page = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> data = taskManagementService.page(param, page, size);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get page error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 根据id查询任务信息
     *
     * @param id
     * @return
     */
    @GetMapping("getById")
    public RestResponse getById(String id) {
        try {
            LifelineTaskEntity entity = taskManagementService.getById(id);
            return RestResponse.succeed(taskManagementService.entityToVo(entity));
        } catch (Exception e) {
            log.error("get getById error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 设为当前任务
     */
    @GetMapping("currentTaskById")
    public RestResponse currentTaskById(String id) {
        try {
            taskManagementService.currentTaskById(id);
            return RestResponse.succeed("设为当前任务成功!");
        } catch (Exception e) {
            log.error("get currentTaskById error:{}", e.getMessage(), e);
            return RestResponse.fail("设为当前任务失败!");
        }
    }

    /**
     * 删除任务
     */
    @GetMapping("deleteById")
    public RestResponse deleteById(String id) {
        try {
            LifelineTaskEntity entity = taskManagementService.getById(id);
            if (entity.getCurrentTask() == 1) {
                return RestResponse.fail("该任务为当前任务,不允许删除!");
            }
            taskManagementService.deleteById(id);
            return RestResponse.succeed("删除任务成功!");
        } catch (Exception e) {
            log.error("get deleteById error:{}", e.getMessage(), e);
            return RestResponse.fail("删除任务失败!");
        }
    }

    /**
     * 综合检索模糊查询
     *
     * @param param
     * @param request
     * @return
     */
    @GetMapping("fuzzyQuery")
    public RestResponse fuzzyQuery(FuzzyQueryParam param, HttpServletRequest request) {
        try {
            int page = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> data = taskManagementService.fuzzyQuery(param, page, size);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("get fuzzyQuery error:{}", e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }
}
